import java.util.concurrent.locks.ReentrantLock;

public class Mthread2 implements Runnable{
    private int share_value = 20;
    private final ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {

//    public synchronized void run() {
//        synchronized (this) {
//           while(share_value > 0) {
//               System.out.println(Thread.currentThread().getName() + " " + share_value);
//               share_value -- ;
//           }
//        }
            while (true) {
                try {
                    lock.lock();

                    if (share_value > 0) {
                        System.out.println(Thread.currentThread().getName() + " " + share_value);
                        share_value--;
                    }else break;

                }catch (Exception e)
                {}
                finally  {
                    lock.unlock();
                }
            }
        }


}
